package com.pk.platform.business.data.service.impl;

import com.pk.platform.business.data.dao.IPackDao;
import com.pk.platform.business.data.service.IPackService;
import com.pk.platform.domain.data.Pack;
import com.pk.platform.util.GlobalLoader;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.constant.Constant;

public class PackServiceImpl implements IPackService {

	private IPackDao packDao;
	
	@Override
	public void delPack(String id) {
		packDao.delete(Pack.class, id);
	}

	@Override
	public Pack queryPackById(String id) {
		return packDao.get(Pack.class, id);
	}
	
	public Pack queryPackByName(String name)
	{
		return packDao.queryPackByName(name);
	}

	@Override
	public synchronized String savePack(Pack pack) {
		String success = StringConverter.resultStr(Constant.SUCCESS, null);
		String failure = StringConverter.resultStr(Constant.FAILURE, "已存在该包名称，请重新输入。");
		String result = success;
		int count = packDao.queryPackNameCount(pack.getPackName(), pack.getId());
		if(StringConverter.isNotNull(pack.getId()))
		{
			if(count > 0)
			{
				result = failure;
			}
			else
			{
				Pack pa = this.queryPackById(pack.getId());
				pa.setPackName(pack.getPackName());
				pa.setPackBody(StringConverter.stringToBlob(pack.getPackBodyStr()));
				if(Constant.BASELIB.equals(pack.getPackName()))
				{
					GlobalLoader.getContext().setAttribute(Constant.BASELIB, pack.getPackBodyStr());
				}
			}
		}
		else
		{
			if(count > 0)
			{
				result = failure;
			}
			else
			{
				packDao.add(pack);
			}
		}
		return result;
	}

	public IPackDao getPackDao() {
		return packDao;
	}

	public void setPackDao(IPackDao packDao) {
		this.packDao = packDao;
	}

}
